//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Text.RegularExpressions;
using System.Web.UI;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Modules.NavigationProvider;
using DotNetNuke.UI.Modules;
using DotNetNuke.UI.WebControls;
namespace DotNetNuke.UI.Containers
{
	public class ActionsMenu : Control, IActionControl
	{
		private ActionManager _ActionManager;
		private ModuleAction _ActionRoot;
		private IModuleControl _ModuleControl;
		private int _ExpandDepth = -1;
		private string _PathSystemScript;
		private bool _PopulateNodesFromClient = false;
		private NavigationProvider _ProviderControl;
		private string _ProviderName = "DNNMenuNavigationProvider";
		protected ModuleAction ActionRoot {
			get {
				if (_ActionRoot == null) {
					_ActionRoot = new ModuleAction(ModuleControl.ModuleContext.GetNextActionID(), " ", "", "", "action.gif");
				}
				return _ActionRoot;
			}
		}
		protected NavigationProvider ProviderControl {
			get { return _ProviderControl; }
		}
		public int ExpandDepth {
			get {
				if (PopulateNodesFromClient == false || ProviderControl.SupportsPopulateOnDemand == false) {
					return -1;
				}
				return _ExpandDepth;
			}
			set { _ExpandDepth= value; }
		}
		public string PathSystemScript {
			get { return _PathSystemScript; }
			set { _PathSystemScript= value; }
		}
		public bool PopulateNodesFromClient {
			get { return _PopulateNodesFromClient; }
			set { _PopulateNodesFromClient= value; }
		}
		public string ProviderName {
			get { return _ProviderName; }
			set { _ProviderName= value; }
		}
		private void BindMenu(DNNNodeCollection objNodes)
		{
			this.Visible = ActionManager.DisplayControl(objNodes);
			if (this.Visible) {
				this.ProviderControl.ClearNodes();
				foreach (DNNNode objNode in objNodes) {
					ProcessNodes(objNode);
				}
				ProviderControl.Bind(objNodes);
			}
		}
		private void ProcessNodes(DNNNode objParent)
		{
			if (!String.IsNullOrEmpty(objParent.JSFunction)) {
				objParent.JSFunction = string.Format("if({0}){{{1}}};", objParent.JSFunction, Page.ClientScript.GetPostBackEventReference(ProviderControl.NavigationControl, objParent.ID));
			}
			foreach (DNNNode objNode in objParent.DNNNodes) {
				ProcessNodes(objNode);
			}
		}
		private void SetMenuDefaults()
		{
			try {
				ProviderControl.StyleIconWidth = 15;
				ProviderControl.MouseOutHideDelay = 500;
				ProviderControl.MouseOverAction = NavigationProvider.HoverAction.Expand;
				ProviderControl.MouseOverDisplay = NavigationProvider.HoverDisplay.None;
				ProviderControl.CSSControl = "ModuleTitle_MenuBar";
				ProviderControl.CSSContainerRoot = "ModuleTitle_MenuContainer";
				ProviderControl.CSSNode = "ModuleTitle_MenuItem";
				ProviderControl.CSSIcon = "ModuleTitle_MenuIcon";
				ProviderControl.CSSContainerSub = "ModuleTitle_SubMenu";
				ProviderControl.CSSBreak = "ModuleTitle_MenuBreak";
				ProviderControl.CSSNodeHover = "ModuleTitle_MenuItemSel";
				ProviderControl.CSSIndicateChildSub = "ModuleTitle_MenuArrow";
				ProviderControl.CSSIndicateChildRoot = "ModuleTitle_RootMenuArrow";
				if (String.IsNullOrEmpty(ProviderControl.PathSystemScript))
					ProviderControl.PathSystemScript = Common.Globals.ApplicationPath + "/Controls/SolpartMenu/";
				ProviderControl.PathImage = Common.Globals.ApplicationPath + "/Images/";
				ProviderControl.PathSystemImage = Common.Globals.ApplicationPath + "/Images/";
				ProviderControl.IndicateChildImageSub = "action_right.gif";
				ProviderControl.IndicateChildren = true;
				ProviderControl.StyleRoot = "background-color: Transparent; font-size: 1pt;";
				ProviderControl.NodeClick += new NavigationProvider.NodeClickEventHandler(MenuItem_Click);
			} catch (Exception exc) {
				DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected void BindMenu()
		{
			BindMenu(Navigation.GetActionNodes(this.ActionRoot, this, this.ExpandDepth));
		}
		protected virtual void OnAction(ActionEventArgs e)
		{
			if (Action != null) {
				Action(this, e);
			}
		}
		protected override void OnInit(System.EventArgs e)
		{
			_ProviderControl = NavigationProvider.Instance(this.ProviderName);
			ProviderControl.PopulateOnDemand += ProviderControl_PopulateOnDemand;
			base.OnInit(e);
			ProviderControl.ControlID = "ctl" + this.ID;
			ProviderControl.Initialize();
			this.Controls.Add(ProviderControl.NavigationControl);
		}
		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			ActionRoot.Actions.AddRange(ModuleControl.ModuleContext.Actions);
			SetMenuDefaults();
			ProviderControl.PopulateOnDemand += new NavigationProvider.PopulateOnDemandEventHandler(ProviderControl_PopulateOnDemand);
		}
		protected override void OnPreRender(System.EventArgs e)
		{
			base.OnPreRender(e);
			BindMenu();
		}
		private void MenuItem_Click(NavigationEventArgs args)
		{
			if (Regex.IsMatch(args.ID,"^\\d+$")) {
				ModuleAction action = ModuleControl.ModuleContext.Actions.GetActionByID(Convert.ToInt32(args.ID));
				if (!ActionManager.ProcessAction(action)) {
					OnAction(new ActionEventArgs(action, ModuleControl.ModuleContext.Configuration));
				}
			}
		}
		private void ProviderControl_PopulateOnDemand(NavigationEventArgs args)
		{
			SetMenuDefaults();
			ActionRoot.Actions.AddRange(ModuleControl.ModuleContext.Actions);
			Entities.Modules.Actions.ModuleAction objAction = ActionRoot;
			if (ActionRoot.ID != Convert.ToInt32(args.ID)) {
				objAction = ModuleControl.ModuleContext.Actions.GetActionByID(Convert.ToInt32(args.ID));
			}
			if (args.Node == null) {
				args.Node = Navigation.GetActionNode(args.ID, ProviderControl.ID, objAction, this);
			}
			this.ProviderControl.ClearNodes();
			this.BindMenu(Navigation.GetActionNodes(objAction, args.Node, this, this.ExpandDepth));
		}
		public event ActionEventHandler Action;
		public ActionManager ActionManager {
			get {
				if (_ActionManager == null) {
					_ActionManager = new ActionManager(this);
				}
				return _ActionManager;
			}
		}
		public IModuleControl ModuleControl {
			get { return _ModuleControl; }
			set { _ModuleControl = value; }
		}
	}
}
